/*
 * Filename: NetworkService.java
 * Description: This class provides all related functions about network connectivity. 
 */

package edu.usc.libtalk.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import edu.usc.libtalk.SignInWithNameActivity;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

public class ApplicationService extends Application implements InternalConstant{

	private static Context context;
	private static ConnectivityManager connectionManager;
	private static SharedPreferences sharedPref;
	private static SharedPreferences.Editor editor;
	private static final String TAG = "ApplicationService";	

	public static Activity register(Activity activity){
		
		if(context == null){
			context = activity.getApplicationContext();
		}
		
		return activity;
	}	
	
	@Override
	public void onCreate() {
		
		context = getApplicationContext();
		connectionManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		
        sharedPref = context.getSharedPreferences(VALUES_FILE_KEY, Context.MODE_PRIVATE);
        editor = sharedPref.edit();
        cleanUpLocal();

	}
	
	public static void cleanUpLocal() {		
        // Reset saved variables
        editor.putString(VALUES_FILE_KEY_USERNAME, "");
        editor.putString(VALUES_FILE_KEY_LIBRARY, "");
        editor.putInt(VALUES_FILE_KEY_LEVEL, -1);
        editor.putInt(VALUES_FILE_KEY_SEAT, -1);
        editor.putString(VALUES_FILE_KEY_INTEREST, "");
        editor.commit();
	}

	public static void setUserName(String username) {	
        editor.putString(VALUES_FILE_KEY_USERNAME, username);
        editor.commit();
	}
	
	public static String getUserName() {	
		return sharedPref.getString(VALUES_FILE_KEY_USERNAME, null);
	}	
	
	public static void setLibrary(String library) {	
        editor.putString(VALUES_FILE_KEY_LIBRARY, library);
        editor.commit();
	}
	
	public static String getLibrary() {	
		return sharedPref.getString(VALUES_FILE_KEY_LIBRARY, null);
	}
	
	public static void setLevel(int level) {	
        editor.putInt(VALUES_FILE_KEY_LEVEL, level);
        editor.commit();
	}
	
	public static int getLevel() {	
		return sharedPref.getInt(VALUES_FILE_KEY_LEVEL, -1);
	}
	
	public static void setSeat(int seat) {	
        editor.putInt(VALUES_FILE_KEY_SEAT, seat);
        editor.commit();
	}
	
	public static int getSeat() {	
		return sharedPref.getInt(VALUES_FILE_KEY_SEAT, -1);
	}	
	
	public static void setDeviceID(String deviceID) {	
        editor.putString(VALUES_FILE_KEY_DEVICE_ID, deviceID);
        editor.commit();
	}
	
	public static String getDeviceID() {	
		return sharedPref.getString(VALUES_FILE_KEY_DEVICE_ID, null);
	}	

	public static void setAreaOfInterest(String interest) {	
        editor.putString(VALUES_FILE_KEY_INTEREST, interest);
        editor.commit();
	}
	
	public static String getAreaOfInterest() {	
		return sharedPref.getString(VALUES_FILE_KEY_INTEREST, null);
	}		
	
	public static boolean hasInternetConnection() {		
		return connectionManager != null 
				&& connectionManager.getActiveNetworkInfo() != null
				&& connectionManager.getActiveNetworkInfo().isConnectedOrConnecting();
	}
	
	public static String executeHttpGet(String url) {
		BufferedReader in = null;

		if(!hasInternetConnection()) {
			Log.e(TAG, "executeHttpGet: no the Internet connection");
			return null;
		}
		
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(url);
			
			HttpResponse response = client.execute(request);
			
			in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			
			return sb.toString();
			
		} catch(Exception e) { 
			 Log.e(TAG, "executeHttpGet: " + e.toString());
		}
		
		return null;
	}
	
	public static void inform(String message){
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}
	
	public static boolean isEmailValid(String email) {
	    boolean isValid = false;

	    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
	    CharSequence inputStr = email;

	    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
	    Matcher matcher = pattern.matcher(inputStr);
	    if (matcher.matches()) {
	        isValid = true;
	    }
	    return isValid;
	}

}
